package com.example.exam.MeunFrgment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.fragment.app.Fragment;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.exam.Entity.Shop;
import com.example.exam.R;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopFragment extends Fragment implements View.OnClickListener {
    private String s = "[\n" +
            "    {\n" +
            "        \"id\": 1, \n" +
            "        \"name\": \"男士\", \n" +
            "        \"productTypeDtos\": [\n" +
            "            {\n" +
            "                \"id\": 23, \n" +
            "                \"name\": \"上装\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 33, \n" +
            "                        \"name\": \"毛衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 34, \n" +
            "                        \"name\": \"T恤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 35, \n" +
            "                        \"name\": \"衬衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 36, \n" +
            "                        \"name\": \"卫衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 37, \n" +
            "                        \"name\": \"背心\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 38, \n" +
            "                        \"name\": \"针织衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 39, \n" +
            "                        \"name\": \"POLO衫\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 24, \n" +
            "                \"name\": \"裤装\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 40, \n" +
            "                        \"name\": \"背带裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 41, \n" +
            "                        \"name\": \"休闲裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 42, \n" +
            "                        \"name\": \"短裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 43, \n" +
            "                        \"name\": \"运动裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 44, \n" +
            "                        \"name\": \"哈伦裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 45, \n" +
            "                        \"name\": \"九分裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 46, \n" +
            "                        \"name\": \"七分裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 47, \n" +
            "                        \"name\": \"西裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 48, \n" +
            "                        \"name\": \"牛仔裤\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 25, \n" +
            "                \"name\": \"外套\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 49, \n" +
            "                        \"name\": \"皮衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 50, \n" +
            "                        \"name\": \"风衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 51, \n" +
            "                        \"name\": \"夹克\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 52, \n" +
            "                        \"name\": \"便西\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 53, \n" +
            "                        \"name\": \"马甲\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 54, \n" +
            "                        \"name\": \"大衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 55, \n" +
            "                        \"name\": \"羽绒服\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }\n" +
            "        ]\n" +
            "    }, \n" +
            "    {\n" +
            "        \"id\": 2, \n" +
            "        \"name\": \"女士\", \n" +
            "        \"productTypeDtos\": [\n" +
            "            {\n" +
            "                \"id\": 26, \n" +
            "                \"name\": \"上装\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 56, \n" +
            "                        \"name\": \"T恤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 57, \n" +
            "                        \"name\": \"衬衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 58, \n" +
            "                        \"name\": \"上衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 59, \n" +
            "                        \"name\": \"卫衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 60, \n" +
            "                        \"name\": \"雪纺衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 61, \n" +
            "                        \"name\": \"针织衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 62, \n" +
            "                        \"name\": \"背心\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 27, \n" +
            "                \"name\": \"裤装\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 63, \n" +
            "                        \"name\": \"打底裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 64, \n" +
            "                        \"name\": \"休闲裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 65, \n" +
            "                        \"name\": \"牛仔裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 66, \n" +
            "                        \"name\": \"短裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 67, \n" +
            "                        \"name\": \"连体裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 68, \n" +
            "                        \"name\": \"七分裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 69, \n" +
            "                        \"name\": \"九分裤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 70, \n" +
            "                        \"name\": \"哈伦裤\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 29, \n" +
            "                \"name\": \"裙装\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 71, \n" +
            "                        \"name\": \"连衣裙\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 72, \n" +
            "                        \"name\": \"半身裙\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 73, \n" +
            "                        \"name\": \"套裙\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 74, \n" +
            "                        \"name\": \"迷你短裙\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 75, \n" +
            "                        \"name\": \"礼服\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 28, \n" +
            "                \"name\": \"外套\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 76, \n" +
            "                        \"name\": \"风衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 77, \n" +
            "                        \"name\": \"便西\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 78, \n" +
            "                        \"name\": \"夹克\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 79, \n" +
            "                        \"name\": \"马甲\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 80, \n" +
            "                        \"name\": \"斗篷\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 81, \n" +
            "                        \"name\": \"大衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 82, \n" +
            "                        \"name\": \"羽绒服\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 83, \n" +
            "                        \"name\": \"皮草\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 30, \n" +
            "                \"name\": \"内衣\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 84, \n" +
            "                        \"name\": \"内衣\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }\n" +
            "        ]\n" +
            "    }, \n" +
            "    {\n" +
            "        \"id\": 3, \n" +
            "        \"name\": \"童装\", \n" +
            "        \"productTypeDtos\": [\n" +
            "            {\n" +
            "                \"id\": 31, \n" +
            "                \"name\": \"男童\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 85, \n" +
            "                        \"name\": \"T恤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 86, \n" +
            "                        \"name\": \"外套\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 87, \n" +
            "                        \"name\": \"卫衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 88, \n" +
            "                        \"name\": \"衬衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 89, \n" +
            "                        \"name\": \"裤子\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }, \n" +
            "            {\n" +
            "                \"id\": 32, \n" +
            "                \"name\": \"女童\", \n" +
            "                \"productTypeDtos\": [\n" +
            "                    {\n" +
            "                        \"id\": 90, \n" +
            "                        \"name\": \"连衣裙\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 91, \n" +
            "                        \"name\": \"T恤\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 92, \n" +
            "                        \"name\": \"卫衣\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 93, \n" +
            "                        \"name\": \"背心\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 94, \n" +
            "                        \"name\": \"礼服\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 95, \n" +
            "                        \"name\": \"雪纺衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 96, \n" +
            "                        \"name\": \"衬衫\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 97, \n" +
            "                        \"name\": \"裤子\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 98, \n" +
            "                        \"name\": \"半身裙\"\n" +
            "                    }, \n" +
            "                    {\n" +
            "                        \"id\": 99, \n" +
            "                        \"name\": \"外套\"\n" +
            "                    }\n" +
            "                ]\n" +
            "            }\n" +
            "        ]\n" +
            "    }\n" +
            "]\n";
    private View view;
    private List<Shop> list = new ArrayList<>();
    private List<String> options1Item = new ArrayList<>();
    private List<List<String>> options2Item = new ArrayList<>();
    private List<List<List<String>>> options3Item = new ArrayList<>();
    private OptionsPickerView options;
    private Button button_shop;

    public ShopFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_shop, container, false);
        initData();
        initView();
        return view;
    }

    private void initData() {
        JsonArray jsonElements = new JsonParser().parse(s).getAsJsonArray();
        for (JsonElement bean : jsonElements) {
            Shop shop = new Gson().fromJson(bean, Shop.class);
            list.add(shop);
        }
        for (int i = 0; i < list.size(); i++) {
            options1Item.add(list.get(i).getName());
            List<String> item2 = new ArrayList<>();
            List<List<String>> item3 = new ArrayList<>();
            for (int j = 0; j < list.get(i).getProductTypeDtos().size(); j++) {
                item2.add(list.get(i).getProductTypeDtos().get(j).getName());
                List<String> item3item = new ArrayList<>();
                for (int q = 0; q < list.get(i).getProductTypeDtos().get(j).getProductTypeDtos().size(); q++) {
                    item3item.add(list.get(i).getProductTypeDtos().get(j).getProductTypeDtos().get(q).getName());
                }
                item3.add(item3item);
            }
            options2Item.add(item2);
            options3Item.add(item3);
        }
    }

    private void initView() {
        button_shop=view.findViewById(R.id.button_shop);
        button_shop.setOnClickListener(this);
        options = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                button_shop.setText(options1Item.get(options1)
                        +options2Item.get(options1).get(options2)
                        + options3Item.get(options1).get(options2).get(options3));
            }
        }).build();
        options.setPicker(options1Item,options2Item,options3Item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_shop:
                options.show();
                break;
        }
    }
}
